package com.csair.seam.domain.service.impl;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.csair.seam.domain.mapper.NewAddAndScrapMapper;
import com.csair.seam.domain.model.entity.NewAddAndScrap;
import com.csair.seam.domain.model.param.EquipParam;
import com.csair.seam.domain.model.vo.NewAddAndScrapVo;
import com.csair.seam.domain.service.INewAddAndScrapService;
@Service
public class NewAddAndScrapServiceImp implements INewAddAndScrapService {
	
	@Autowired
	private NewAddAndScrapMapper newAddAndScrapMapper;

	@DS("nc63")
	@Override
	public List<NewAddAndScrapVo> findNewAddAndScrap(EquipParam param) {
		Map<String, NewAddAndScrapVo> map = new HashMap<>();
		List<NewAddAndScrap> newAddList = newAddAndScrapMapper.findNewAdd(param);//新增设备数据
		for (NewAddAndScrap newAddAndScrap : newAddList) {
			NewAddAndScrapVo vo = new NewAddAndScrapVo();
			vo.setOrgName(newAddAndScrap.getOrgName());
			vo.setAddTotalNum(newAddAndScrap.getTotalNum());
			vo.setAddToalPrice(newAddAndScrap.getToalPrice());
			map.put(newAddAndScrap.getOrgCode(), vo);
		}
		List<NewAddAndScrap> scrapList = newAddAndScrapMapper.findScrap(param);//报废设备数据
		for (NewAddAndScrap newAddAndScrap : scrapList) {
			if(map.containsKey(newAddAndScrap.getOrgCode())) {
				NewAddAndScrapVo vo = map.get(newAddAndScrap.getOrgCode());
				vo.setScrapTotalNum(newAddAndScrap.getTotalNum());
				vo.setScrapToalPrice(newAddAndScrap.getToalPrice());
			}else {
				NewAddAndScrapVo vo = new NewAddAndScrapVo();
				vo.setOrgName(newAddAndScrap.getOrgName());
				vo.setScrapTotalNum(newAddAndScrap.getTotalNum());
				vo.setScrapToalPrice(newAddAndScrap.getToalPrice());
				map.put(newAddAndScrap.getOrgCode(), vo);
			}
		}
		
		//按新增数量降序排序
		if(!map.isEmpty()) {
			return map.values().stream().sorted(Comparator.comparing(NewAddAndScrapVo::getAddTotalNum).reversed()).collect(Collectors.toList());
		}
		return Collections.emptyList();
	}

}
